package com.cq.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.cq.health.common.MessageConst;
import com.cq.health.entity.Result;
import com.cq.health.pojo.OrderSetting;
import com.cq.health.service.OrderSettingService;
import com.cq.health.utils.POIUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

/**
 * @Author: 悠久的苍穹
 * @Date: 2020/6/2 12:34
 * @Description: 预约设置控制器
 * @Version: 1.0
 */
@RestController
@Slf4j
@RequestMapping("ordersetting")
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;

    /**
     * 上传excel
     * @param multipartFile
     * @return
     */
    @RequestMapping("upload")
    public Result upload(@RequestParam("excelFile") MultipartFile multipartFile){
        try {
            String fileName = multipartFile.getName();
            log.debug("excel fileName:"+fileName);
            //从excel读取原数据
            List<String[]> list = POIUtils.readExcel(multipartFile);
            //把excel转换为 List<OrderSetting> ,遍历数据
            List<OrderSetting> orderSettingList = new ArrayList<>();
            for (String[] row : list) {
                if(row[0] == null){
                    continue;
                }
                log.debug("excel row[0]:{},row[1]:{}",row[0],row[1]);
                OrderSetting orderSetting = new OrderSetting();
                orderSetting.setOrderDate(new Date(row[0]));
                orderSetting.setNumber(Integer.parseInt(row[1]));
                orderSettingList.add(orderSetting);
                orderSettingService.add(orderSettingList);
            }
            return new Result(true, MessageConst.IMPORT_ORDERSETTING_SUCCESS);
        }catch (Exception e){
             e.printStackTrace();
            return new Result(false, MessageConst.IMPORT_ORDERSETTING_FAIL);
        }
    }

    @RequestMapping("getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String date){
        try {
            log.debug("getOrderSettingByMonth date:"+date);
            List<Map> mapList = orderSettingService.getOrderSettingByMonth(date);
            return new Result(true,MessageConst.GET_ORDERSETTING_SUCCESS,mapList);
        }catch (Exception e){
             e.printStackTrace();
             return new Result(false,MessageConst.GET_ORDERSETTING_FAIL);
        }
    }

    @RequestMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
        log.debug("editNumberByDate orderSetting:"+orderSetting);
        try {
            orderSettingService.editOrderSettingByDate(orderSetting);
            return new Result(true,MessageConst.ACTION_SUCCESS);
        }catch (Exception e){
             e.printStackTrace();
             return new Result(false,MessageConst.ACTION_FAIL);
        }
    }
}
